package com.yiibai.netty.time.client;

import java.text.SimpleDateFormat;
import java.util.Date;

import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;

public class TimeClientHandler extends ChannelInboundHandlerAdapter{

	@Override
	public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
		ByteBuf m = (ByteBuf) msg; 
		try {
			long currentTimeMillis = (m.readUnsignedInt() - 2208988800L) * 1000L;
			Date currentTime = new Date(currentTimeMillis);
			System.out.println("Default Date Format:" + currentTime.toString());
			
			SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			String dateString = format.format(currentTime);
			System.out.println("Date format:" + dateString);
			
			ctx.close();
		} finally {
			m.release();
		}

	}

	@Override
	public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
		cause.printStackTrace();
		ctx.close();
	}

}
